package com.example.my.agiftthat.ui.classify.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.my.agiftthat.R;
import com.example.my.agiftthat.config.Url;
import com.example.my.agiftthat.helper.OkHttpClientHelper;
import com.example.my.agiftthat.ui.classify.adapter.SingleRightAdapter;
import com.example.my.agiftthat.ui.classify.bean.SingleBean;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by My on 2016/12/22.
 */

public class SingleRightFragment extends Fragment {
    private Context mContext = null;
    private RecyclerView recyclerView_single_right;
    private List<SingleBean.DataBean.CategoriesBean.SubcategoriesBean> mList = new ArrayList<>();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    mList = (List<SingleBean.DataBean.CategoriesBean.SubcategoriesBean>) msg.obj;
                    recyclerView_single_right.setHasFixedSize(true);
                    recyclerView_single_right.setLayoutManager(new GridLayoutManager(mContext, 3));
                    SingleRightAdapter adapter = new SingleRightAdapter(mContext, mList);
                    recyclerView_single_right.setAdapter(adapter);
                    Log.i("tag","========================");
                    break;
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        reLoad();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.singlefragment_right, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView_single_right = (RecyclerView) view.findViewById(R.id.recyclerView_single_right);
    }

    private void reLoad() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String json = OkHttpClientHelper.getStringFromURL(getActivity(), Url.CLASSIFY_DP, 1);
                    Gson gson = new Gson();
                    SingleBean bean = gson.fromJson(json, SingleBean.class);
                    mList = bean.getData().getCategories().get(0).getSubcategories();

                    Message msg = new Message();
                    msg.what = 0;
                    msg.obj = mList;
                    handler.sendMessage(msg);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        Log.i("aaaa","走过");
//        Bundle bundle=getArguments();
//        int position = bundle.getInt("position");
//        this.position = position;
//        Log.i("aaaa","!!!!!!!!!!!!!!!"+position);
//        textView_right.setText("position"+position);
    }
}
